//
//  CXHUBiMAXRewardAdapter.m
//  CXHAdSDK-UBiMAX
//
//  Created by 陈坤 on 2025/7/28.
//

#import "CXHUBiMAXRewardAdapter.h"
#import <UBiMAXAdSDK/UBiMAXAdSDK-umbrella.h>
#import <CXHAdSDK-Core/CXHAdSDKRewardVideoAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHUBiMAXRewardAdapter ()<UMTCustomRewardedVideoAdapter, CXHAdSDKRewardVideoAdDelegate>
@property (nonatomic, strong) CXHAdSDKRewardVideoAd *rewardedVideoAd;
@property (nonatomic, assign) BOOL isLoadSucc;

@property (nonatomic, assign) UMTAdBidType umtBidType;
@property (nonatomic, assign) UMTVideoMuteType umtVideoMuteType;

@end

@implementation CXHUBiMAXRewardAdapter

- (UMTMediatedAdStatus)mediatedAdStatus {
    if (self.isLoadSucc && self.rewardedVideoAd.isAdValid) {
        return UMTMediatedAdStatusNormal;
    }
    return UMTMediatedAdStatusUnknown;
}

/// 加载广告的方法
/// @param slotID adn的广告位ID
/// @param parameter 广告加载的参数
- (void)loadRewardedVideoAdWithSlotID:(NSString *)slotID andParameter:(NSDictionary *)parameter {
    NSNumber *bidType = parameter[UMTAdLoadingParamBiddingType];
    self.umtBidType = (UMTAdBidType)bidType.integerValue;
    NSNumber *videoMuteNum = parameter[UMTAdLoadingParamVideoMute];
    self.umtVideoMuteType = (UMTVideoMuteType)videoMuteNum.integerValue;
    
    if (self.rewardedVideoAd) {
        self.rewardedVideoAd.delegate = nil;
        self.rewardedVideoAd = nil;
    }
    
    self.rewardedVideoAd = [[CXHAdSDKRewardVideoAd alloc] init];
    self.rewardedVideoAd.delegate = self;
    if (self.umtVideoMuteType == UMTVideoMuteType_Mute) {
        self.rewardedVideoAd.videoMuted = true;
    } else {
        self.rewardedVideoAd.videoMuted = false;
    }
    self.rewardedVideoAd.posId = slotID;
    [self.rewardedVideoAd loadAdData];
}

/// 展示广告的方法
/// @param viewController 控制器对象
/// @param parameter 展示广告的参数，接入媒体配置
- (void)showAdFromRootViewController:(UIViewController *_Nonnull)viewController parameter:(NSDictionary *)parameter {
    [self.rewardedVideoAd showRewardVideoAdFromRootViewController:viewController];
}

- (void)didReceiveBidResult:(UMTMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        NSInteger winPrice = self.rewardedVideoAd.getBidPrice;
        if (result.winnerPrice) {
            winPrice = result.winnerPrice;
        }
        [self.rewardedVideoAd sendWinNotice:winPrice];
    } else {
        if (result.winnerPrice) {
            [self.rewardedVideoAd sendLossNotice:result.winnerPrice];
        }
    }
}

- (void)destroyAd { 
    
}
#pragma mark - CXHAdSDKRewardVideoAdDelegate
/**
 广告数据加载成功回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidLoad:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    self.isLoadSucc = YES;
//    NSDictionary *mediaExtra = rewardedVideoAd.mediaExtraInfo;
    
    NSMutableDictionary *extDic = [NSMutableDictionary dictionary];
    if (self.umtBidType == UMTAdBidType_ClientBidding) {
        NSInteger price = [self.rewardedVideoAd getBidPrice];
        if (price>0) {
            [extDic setValue:[NSString stringWithFormat:@"%ld", price] forKey:UMTAdnAdLoadedExtECPM];
        }
    }
    
    [self.bridge rewardedVideoAd:self videoDidLoadWithExt:extDic];
    [self.bridge rewardedVideoAd:self didLoadWithExt:@{}];
}

///**
// 视频数据下载成功回调，已经下载过的视频会直接回调
//
// @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
// */
//- (void)CXH_rewardVideoAdVideoDidLoad:(CXHAdSDKRewardVideoAd *)rewardedVideoAd;

/**
 视频播放页即将展示回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdWillVisible:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频广告曝光回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidExposed:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    [self.bridge rewardedVideoAdDidVisible:self];
}

/**
 视频广告曝光上报回调（只会回调一次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidExposeReport:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频播放页关闭回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClose:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    [self.bridge rewardedVideoAdDidClose:self];
}

/**
 视频广告信息点击回调（可能回调多次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClicked:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    [self.bridge rewardedVideoAdDidClick:self];
    [self.bridge rewardedVideoAdWillPresentFullScreenModel:self];
}

/**
 视频广告信息点击上报回调（只会回调一次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClickReport:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频广告各种错误信息回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 @param error 具体错误信息
 */
- (void)CXH_rewardVideoAd:(CXHAdSDKRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    [self.bridge rewardedVideoAd:self didLoadFailWithError:error ext:@{}];
}


/**
 视频广告播放达到激励条件回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidRewardEffective:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    [self.bridge rewardedVideoAd:self didServerRewardSuccessWithInfo:^(UMTAdapterRewardAdInfo *info) {
        
    }];
}

/**
 视频广告视频播放完成

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidPlayFinish:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    [self.bridge rewardedVideoAd:self didPlayFinishWithError:nil];
}

@end
